package com.crl.module.cmpRpc
{
	import com.crl.core.config.ConfigurationManager;
	import com.crl.core.config.section.model.ConnectionStringSetting;
	import com.crl.module.cmpRpc.behavior.AbstractRequestBehavior;
	import com.crl.module.cmpRpc.factory.ServiceFactory;
	import com.crl.module.cmpRpc.model.RequestVO;
	import com.crl.module.cmpRpc.section.RPCSectionHandler;
	import com.crl.utils.StringUtil;
	
	import flash.utils.Dictionary;
	
	RPCSectionHandler;
	/**
	 *  远程方法外观, 通过外观模式,客户端不必知道,请求是由谁来提供服务,只要知道要传递相应的参数就行了.
	 * @author crl
	 * 
	 */	
	public class Provider
	{
		private var requestList:Dictionary;
		
		public function Provider()
		{
			
			requestList=ConfigurationManager.getSection("rpc") as Dictionary;
		}
		
		static private var instance:Provider;
		static public function getInstance():Provider{
			
			if(instance==null)
			{
				instance=new Provider();
			}
			return instance;
		}
		
		
		/**
		 *  请求远程方法;
		 * @param name
		 * @param parms
		 * @return 
		 * 
		 */		
		public function get(name:String):AbstractRequestBehavior{
			
			var requestvo:RequestVO=requestList[name];
			
			if(requestvo==null){
				throw new Error(StringUtil.substitute("你所请求的方法:{0},并没有正确的配置",name));
			}
			
			var key:String=requestvo.connectionKey;
			
			var connectStringSetting:ConnectionStringSetting=ConfigurationManager.connectionStrings[key] as ConnectionStringSetting;
			if(connectStringSetting==null){
				throw new Error(StringUtil.substitute("你所请求的方法:{0},并没有正常的配置服务类型:{1}",name,key));
			}
			
		
			var behavior:AbstractRequestBehavior=ServiceFactory.create(connectStringSetting);
			behavior.requestvo=requestvo;
			
			return behavior;
		}
		
	}
}